2.6 比较、掩码和布尔逻辑

本文源码请见我的GitHub

1
2
import pandas as pd
import numpy as np
1
rainfall = pd.read_csv('./Seattle2014.csv')
1
rainfall =rainfall['PRCP'].values
1
2
inches = rainfall / 254 #1/1mm ->inches
inches.shape
(365,)
1
2
3
%matplotlib inline
import matplotlib.pyplot as plt
import seaborn as sns; sns.set()
1
plt.hist(inches, 40);

2.6.2 和通用函数类似的比较操作

1
x = np.array([1, 2, 3, 4, 5])
1
x < 3
array([ True,  True, False, False, False])
1
x > 3
array([False, False, False,  True,  True])
1
x <= 3
array([ True,  True,  True, False, False])
1
x!=3
array([ True,  True, False,  True,  True])
1
x == 3
array([False, False,  True, False, False])
1
(2 * x)  == (x ** 2)
array([False,  True, False, False, False])
1
2
3
rng = np.random.RandomState(0)
x = rng.randint(10, size = (3, 4))
x
array([[5, 0, 3, 3],
       [7, 9, 3, 5],
       [2, 4, 7, 6]])
1
x < 6
array([[ True,  True,  True,  True],
       [False, False,  True,  True],
       [ True,  True, False, False]])

2.6.3 布尔数组

1
print(x)
[[5 0 3 3]
 [7 9 3 5]
 [2 4 7 6]]
1
np.count_nonzero(x < 6)
8
1
np.sum(x < 6)
8
1
np.sum(x < 6, axis=1)
array([4, 2, 2])
1
np.any(x > 8)
True
1
np.any(x < 0)
False
1
np.all(x < 10)
True
1
np.all(x == 6)
False
1
np.all(x < 8, axis = 1)
array([ True, False,  True])
1
np.sum((inches > 0.5) | (inches >= 1))
37
1
2
3
print("number days without rain: ",np.sum(inches == 0))
print("number days with rain",np.sum(inches!=0))
print("days with more than 0.5 inches:",np.sum(inches > 0.5))
number days without rain:  215
number days with rain 150
days with more than 0.5 inches: 37

2.6.4 江布尔数组作为掩码

1
x
array([[5, 0, 3, 3],
       [7, 9, 3, 5],
       [2, 4, 7, 6]])
1
x < 5
array([[False,  True,  True,  True],
       [False, False,  True, False],
       [ True,  True, False, False]])
1
x[x<5]
array([0, 3, 3, 3, 2, 4])